package com.ruoyi.project.mahjong.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.mahjong.domain.TMember;
import com.ruoyi.project.mahjong.domain.dto.ChargeDetailsDto;
import com.ruoyi.project.mahjong.domain.dto.MemberRechargeDto;
import com.ruoyi.project.mahjong.domain.vo.MemberCostRecordVo;
import com.ruoyi.project.mahjong.service.ITMemberService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.TIntegralExchangeMapper;
import com.ruoyi.project.mahjong.domain.TIntegralExchange;
import com.ruoyi.project.mahjong.service.ITIntegralExchangeService;

/**
 * 积分兑换记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-28
 */
@Service
public class TIntegralExchangeServiceImpl implements ITIntegralExchangeService
{
    @Autowired
    private TIntegralExchangeMapper tIntegralExchangeMapper;

    @Autowired
    private ITMemberService memberService;
    /**
     * 查询积分兑换记录
     *
     * @param id 积分兑换记录ID
     * @return 积分兑换记录
     */
    @Override
    public TIntegralExchange selectTIntegralExchangeById(Long id)
    {
        return tIntegralExchangeMapper.selectTIntegralExchangeById(id);
    }

    /**
     * 查询积分兑换记录列表
     *
     * @param tIntegralExchange 积分兑换记录
     * @return 积分兑换记录
     */
    @Override
    public List<TIntegralExchange> selectTIntegralExchangeList(TIntegralExchange tIntegralExchange)
    {
        return tIntegralExchangeMapper.selectTIntegralExchangeList(tIntegralExchange);
    }

    /**
     * 新增积分兑换记录
     *
     * @param tIntegralExchange 积分兑换记录
     * @return 结果
     */
    @Override
    public int insertTIntegralExchange(TIntegralExchange tIntegralExchange)
    {
        return tIntegralExchangeMapper.insertTIntegralExchange(tIntegralExchange);
    }

    /**
     * 修改积分兑换记录
     *
     * @param tIntegralExchange 积分兑换记录
     * @return 结果
     */
    @Override
    public int updateTIntegralExchange(TIntegralExchange tIntegralExchange)
    {
        tIntegralExchange.setUpdateTime(DateUtils.getNowDate());
        return tIntegralExchangeMapper.updateTIntegralExchange(tIntegralExchange);
    }

    /**
     * 批量删除积分兑换记录
     *
     * @param ids 需要删除的积分兑换记录ID
     * @return 结果
     */
    @Override
    public int deleteTIntegralExchangeByIds(Long[] ids)
    {
        return tIntegralExchangeMapper.deleteTIntegralExchangeByIds(ids);
    }

    /**
     * 删除积分兑换记录信息
     *
     * @param id 积分兑换记录ID
     * @return 结果
     */
    @Override
    public int deleteTIntegralExchangeById(Long id)
    {
        return tIntegralExchangeMapper.deleteTIntegralExchangeById(id);
    }

    @Override
    public List<TIntegralExchange> selectTIntegralExchangeByIdAndTime(Long shopId, String startTime, String endTime, String id) {
        return tIntegralExchangeMapper.selectTIntegralExchangeByIdAndTime(shopId,startTime,endTime,id);
    }

    @Override
    public void integralCover(MemberRechargeDto dto, Long shopId, SysUser user) {

        List<ChargeDetailsDto> detailsDtoList=dto.getDtoList();
        TMember member=memberService.selectTMemberById(dto.getUserId());
        Integer sumOfAges = detailsDtoList.stream()
                .mapToInt(ChargeDetailsDto::getIntegral)
                .sum();
        Integer result=Integer.valueOf(ObjectUtil.isNotNull(member.getIntegral())?member.getIntegral():"0")-sumOfAges;
        member.setIntegral(result.toString());
       if(result<0){
            throw  new RuntimeException("积分数量不够，不允许兑换");
        }
        memberService.updateTMember(member);
        for(ChargeDetailsDto rechargeDto:detailsDtoList){
            TIntegralExchange exchange=new TIntegralExchange();
            BeanUtil.copyProperties(rechargeDto,exchange);
            exchange.setUserId(member.getId());
            exchange.setUserName(member.getName());
            exchange.setPhone(member.getPhone());
            exchange.setProduct(rechargeDto.getProduct());
            exchange.setExchangeTime(new Date());
            exchange.setBak1(user.getUserId().toString());
            exchange.setBak2(shopId.toString());
            insertTIntegralExchange(exchange);

        }

    }


}
